#!/usr/bin/php
<?php

require_once("Class/Autoload.php");

$bucketTool = new CLIBucketTool();
$bucketTool->sdbDomainBase("rbox-v2");

$cmd = $argv[0];
$arg1 = isset($argv[1]) ? strtolower($argv[1]) : null;
$arg2 = isset($argv[2]) ? strtolower($argv[2]) : null;

if (is_null($arg1)) {
	echo Errors::BucketTool_MESG_MISSING_OPER_MODE . "\n";
	Errors::helpModes($cmd);
	exit (Errors::BucketTool_CODE_MISSING_OPER_MODE);
}

try {
	switch ($arg1) {
		case "bucket":
			switch ($arg2) {
				case "create":
					$bucketTool->mode("bucketCreate");
					break;
				case "delete":
					$bucketTool->mode("bucketDelete");
					break;
				case "list":
					$bucketTool->mode("bucketListObjects");
					break;
				case "show":
					$bucketTool->mode("bucketsList");
					break;
				case "versioningstatus":
					$bucketTool->mode("bucketVersioningStatus");
					break;
				case "versioningenable":
					$bucketTool->mode("bucketVersioningEnable");
					break;
				case "versioningdisable":
					$bucketTool->mode("bucketVersioningDisable");
					break;
				case "listrawobjects":
					$bucketTool->mode("bucketListRawObjects");
					break;
				case "slowobjectnuke":
					$bucketTool->mode("bucketSlowObjectNuke");
					break;
				case "help":
					Errors::listBucketFunctions($cmd);
					exit(0);
					break;
				default:
					echo Errors::BucketTool_MESG_UNKNOWN_OPER_FUNCTION . "\n";
					Errors::helpBucketFunctions($cmd);
					exit (Errors::BucketTool_CODE_UNKNOWN_OPER_FUNCTION);
					break;
			}
			break;
		case "object":
			switch ($arg2) {
				case "upload":
					$bucketTool->mode("objectUpload");
					break;
				case "download":
					$bucketTool->mode("objectDownload");
					break;
				case "delete":
					$bucketTool->mode("objectDelete");
					break;
				case "getacl":
					$bucketTool->mode("objectGetACL");
					break;
				case "setacl":
					$bucketTool->mode("objectSetACL");
					break;
				case "stat":
					$bucketTool->mode("objectStat");
					break;
				case "url":
					$bucketTool->mode("objectURL");
					break;
				case "storage":
					$bucketTool->mode("objectStorage");
					break;
				case "stalepurge":
					$bucketTool->mode("objectPurgeStales");
					break;
				case "help":
					Errors::listObjectFunctions($cmd);
					exit(0);
					break;
				default:
					echo Errors::BucketTool_MESG_UNKNOWN_OPER_FUNCTION . "\n";
					Errors::helpObjectFunctions($cmd);
					exit (Errors::BucketTool_CODE_UNKNOWN_OPER_FUNCTION);
					break;
			}
			break;
		case "dbase":
			switch ($arg2) {
				case "reset":
					$bucketTool->mode("resetSDB");
					break;
				case "sync":
					$bucketTool->mode("bucketSyncToSDB");
					break;
				case "list":
					$bucketTool->mode("SDBListDomains");
					break;
				case "select":
					$bucketTool->mode("SDBSelect");
					break;
				case "items":
					$bucketTool->mode("SDBListItems");
					break;
				case "help":
					Errors::listDBaseFunctions($cmd);
					exit(0);
					break;
				default:
					echo Errors::BucketTool_MESG_UNKNOWN_OPER_FUNCTION . "\n";
					Errors::helpDBaseFunctions($cmd);
					exit (Errors::BucketTool_CODE_UNKNOWN_OPER_FUNCTION);
					break;
			}
			break;
		case "sync":
			switch ($arg2) {
				case "start":
					$bucketTool->mode("syncStart");
					break;
				case "new":
					$bucketTool->mode("syncNew");
					break;
				case "delete":
					$bucketTool->mode("syncDelete");
					break;
				case "list":
					$bucketTool->mode("syncList");
					break;
				case "oneshot":
					$bucketTool->mode("syncOneShot");
					break;
				case "help":
					Errors::helpSyncFunctions($cmd);
					exit(0);
					break;
				default:
					echo Errors::BucketTool_MESG_UNKNOWN_OPER_FUNCTION . "\n";
					Errors::helpSyncFunctions($cmd);
					exit (Errors::BucketTool_CODE_UNKNOWN_OPER_FUNCTION);
					break;
			}
			break;
		case "awslib":
			switch ($arg2) {
				case "s3version":
					$bucketTool->mode("awslibS3Version");
					break;
				case "s3apiname":
					$bucketTool->mode("awslibS3APIName");
					break;
				case "s3apiuseragent":
					$bucketTool->mode("awslibS3APIUserAgent");
					break;
				case "gpgencrypt":
					$bucketTool->mode("awslibGPGEncrypt");
					break;
				case "gpgdecrypt":
					$bucketTool->mode("awslibGPGDecrypt");
					break;
			default:
				echo Errors::BucketTool_MESG_UNKNOWN_OPER_FUNCTION . "\n";
				Errors::helpAWSLibFunctions($cmd);
				exit (Errors::BucketTool_CODE_UNKNOWN_OPER_FUNCTION);
				break;
			}
			break;
		default:
			echo Errors::BucketTool_MESG_UNKNOWN_OPER_MODE . "\n";
			Errors::helpModes($cmd);
			exit (Errors::BucketTool_CODE_UNKNOWN_OPER_MODE);
	}
} catch (Exception $e) {
	echo "Error: " . $e->getMessage() . "\n";
	Errors::helpModes($cmd);
	exit($e->getCode());
}

// remove the mode and function from the args list
$argv = Utils::array_delete_element($argv, 2);
$argv = Utils::array_delete_element($argv, 1);

try {
	$options = Utils::getopts($argv, $bucketTool->shortOpts, $bucketTool->longOpts);
} catch (Exception $e) {
	echo $e->getMessage() . "\n";
	exit($e->getCode());
}

// process the flags and opts
foreach (((isset($options["opts"])) ? $options["opts"] : array()) as $option => $value) {
	try {
		switch ($option) {
			case "b":
			case "bucket":
				$bucketTool->bucket($value);
				break;
				
			case "d":
			case "folder":
				if ($value[strlen($value) - 1] != "/" ) {
					$value .= "/";
				}
				$bucketTool->remoteFolder($value);
				break;
				
			case "r":
			case "recursive":
				$bucketTool->flag("recursive", true);
				break;
				
			case "s":
			case "standard":
				$bucketTool->flag("storageMode", CLIBucketTool::STORAGE_STANDARD);
				break;
				
			case "public":
				$bucketTool->flag("acl", CLIBucketTool::ACL_PUBLIC);
				break;
			case "private":
				$bucketTool->flag("acl", CLIBucketTool::ACL_PRIVATE);
				break;
			case "open":
				$bucketTool->flag("acl", CLIBucketTool::ACL_OPEN);
				break;
			case "authread":
				$bucketTool->flag("acl", CLIBucketTool::ACL_AUTH_READ);
				break;
			case "ownerread":
				$bucketTool->flag("acl", CLIBucketTool::ACL_OWNER_READ);
				break;
			case "ownerfullcontrol":
				$bucketTool->flag("acl", CLIBucketTool::ACL_OWNER_FULL_CONTROL);
				break;
				
			case "l":
			case "localobject":
				$bucketTool->localObject($value);
				break;
				
			case "o":
			case "remoteobject":
				$bucketTool->remoteObject($value);
				break;
				
			case "i":
			case "versionid":
				$bucketTool->remoteObjectVersionID($value);
				break;
				
			case "a":
			case "sdbdomain":
				$bucketTool->userSDBDomain($value);
				break;

			case "f":
			case "force":
				$bucketTool->flag("force", true);
				break;
				
			case "m":
			case "minutes":
				$bucketTool->minutes($value);
				break;
				
			case "h":
			case "help":
				$bucketTool->flag("help", true);
				break;

			case "uuid":
				$bucketTool->uuid($value);
				break;
				
			case "profilename":
				$bucketTool->profileName($value);
				break;
			
			case "masterlocal":
				$bucketTool->flag("master", SyncProfile::MASTER_LOCAL);
				break;
			
			case "masterremote":
				$bucketTool->flag("master", SyncProfile::MASTER_REMOTE);
				break;
				
			case "this":
				$bucketTool->flag("this", true);
				break;
				
			case "clobber":
				$bucketTool->flag("clobber", true);
				break;
				
			case "progress":
				$bucketTool->flag("progress", true);
				break;
				
			case "strict":
				$bucketTool->flag("strict", true);
				break;
				
			case "V":
			case "verbose":
				$bucketTool->flag("verbose", true);
				break;
		}
	} catch (Exception $e) {
		echo "Error: " . $e->getMessage() . "\n";
		Errors::help();
		exit($e->getCode());
	}
}

if (isset($options["args"])) {
	$bucketTool->args($options["args"]);
}

try {
	$bucketTool->execute();
} catch (Exception $e) {
	echo "Error: " . $e->getMessage() . "\n";
	exit($e->getCode());
}
?>